#include "DHT.h"
#include <ArduinoJson.h>
//定义DHT11相关参数
#define DHTPIN 8
#define DHTTYPE DHT11

//控制客厅开关灯命令
unsigned char light1[8] = {0xFD, 0xF3, 0x03, 0xEA, 0xB6, 0x46, 0x36, 0xDF };
unsigned char light2[8] = {0xFD, 0xF3, 0x03, 0xEA, 0xB6, 0x46, 0x37, 0xDF };
//----------------------------------------------------------------------
unsigned char lightW1[8] = {0xFD, 0xF3, 0x03, 0xE3, 0xBF, 0x4F, 0x36, 0xDF };
unsigned char lightW2[8] = {0xFD, 0xF3, 0x03, 0xE3, 0xBF, 0x4F, 0x37, 0xDF };
//----------------------------------------------------------------------
unsigned char lightY1[8] = {0xFD, 0xF3, 0x03, 0xEF, 0xB0, 0x43, 0x36, 0xDF };
unsigned char lightY2[8] = {0xFD, 0xF3, 0x03, 0xEF, 0xB0, 0x43, 0x37, 0xDF };
//----------------------------------------------------------------------
unsigned char lightTiny1[8] = {0xFD, 0xF3, 0x03, 0xE8, 0xB5, 0x45, 0x36, 0xDF };
unsigned char lightTiny2[8] = {0xFD, 0xF3, 0x03, 0xE8, 0xB5, 0x45, 0x37, 0xDF };
int lightzt = 0;
int lightWzt = 0;
int lightYzt = 0;
int lightTinyzt = 0;

// 初始化DHT11传感器
DHT dht(DHTPIN, DHTTYPE);

//定时计数临时变量
unsigned long lastSend;
//定时时间
int sendMax = 2000 ;
//订阅消息临时变量
String comdata = "";

//初始化开发板
void setup() {
  //初始化WIFI透传串口
  Serial3.begin(115200);
  //初始化信息串口
  Serial.begin(115200);
  //初始化WIFI透传串口
  Serial2.begin(9600);
  //初始化DHT11
  dht.begin();
  lastSend = 0;
  //默认延迟时间等待wifi模块正常工作
  delay(2000);
}

//循环函数 发布数据 接收数据
void loop() {
  //定时发布消息
  if ( millis() - lastSend > sendMax ) { // 用于定时4秒钟发送一次数据
    getDataAndPublic(); // 获取温湿度数据发送到MQTT服务器上去
    lastSend = millis();
  }
  //读取订阅消息
  while (Serial3.available() > 0) {
    comdata += char(Serial3.read());  //每次读一个char字符，并相加
    delay(2);
  }
  if (comdata.length() > 0) {
    Serial.println(comdata); //打印打印接收到的字符
    //判断接收指令
    if (comdata.equals("#LIGHT")) {
      if (lightzt == 0) {
        Serial2.write(light1, 8);
        Serial.println("light1");
        lightzt++;
      } else if (lightzt == 1) {
        Serial2.write(light2, 8);
        lightzt++;
        Serial.println("light2");
      }
      if (lightzt >= 2) {
        lightzt = 0;
      }
      
    }else if(comdata.equals("#LIGHT-W")){
      if (lightWzt == 0) {
        Serial2.write(lightW1, 8);
        Serial.println("lightW1");
        lightWzt++;
      } else if (lightWzt == 1) {
        Serial2.write(lightW2, 8);
        lightWzt++;
        Serial.println("lightW2");
      }
      if (lightWzt >= 2) {
        lightWzt = 0;
      }
    }else if(comdata.equals("#LIGHT-Y")){
     if (lightYzt == 0) {
        Serial2.write(lightY1, 8);
        Serial.println("lightY1");
        lightYzt++;
      } else if (lightYzt == 1) {
        Serial2.write(lightY2, 8);
        lightYzt++;
        Serial.println("lightY2");
      }
      if (lightYzt >= 2) {
        lightYzt = 0;
      }
    }else if(comdata.equals("#LIGHT-Tiny")){
      if (lightTinyzt == 0) {
        Serial2.write(lightTiny1, 8);
        Serial.println("lightTiny1");
        lightTinyzt++;
      } else if (lightTinyzt == 1) {
        Serial2.write(lightTiny2, 8);
        lightTinyzt++;
        Serial.println("lightTiny2");
      }
      if (lightTinyzt >= 2) {
        lightTinyzt = 0;
      }
    }
    //清除接收命令
    comdata = "";
  }
}

void getDataAndPublic()
{
  //1.初始化JSON
  StaticJsonBuffer<300> JSONbuffer;
  JsonObject& JSONencoder = JSONbuffer.createObject();

  //2.DHT11读取 大概250ms读取一次
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  // 查看是否读取温湿度失败的
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  String temperature = String(t);
  String humidity = String(h);
  //拼装JSON
  JSONencoder["temperature"] = temperature;
  JSONencoder["humidity"] = humidity;

  //3.发布消息
  char JSONmessageBuffer[100];
  JSONencoder.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
  Serial.println("[publish]-->>");
  Serial.println(JSONmessageBuffer);
  Serial3.print(JSONmessageBuffer);
  Serial.println("-----------------------------------");
}


